package com.ztorn.fiscale.controller;

import com.ztorn.common.core.utils.poi.ExcelUtil;
import com.ztorn.common.core.web.controller.BaseController;
import com.ztorn.common.core.web.domain.AjaxResult;
import com.ztorn.common.core.web.page.TableDataInfo;
import com.ztorn.common.log.annotation.Log;
import com.ztorn.common.log.enums.BusinessType;
import com.ztorn.common.security.annotation.RequiresPermissions;
import com.ztorn.common.security.utils.SecurityUtils;
import com.ztorn.fiscale.domain.FiscaleProduct;
import com.ztorn.fiscale.service.IFiscaleProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 智能商品库Controller
 * 
 * @author ztorn
 */
@RestController
@RequestMapping("/product")
public class FiscaleProductController extends BaseController {
    @Autowired
    private IFiscaleProductService productService;

    /**
     * 查询智能商品库列表
     */
    @RequiresPermissions("fiscale:product:list")
    @GetMapping("/list")
    public TableDataInfo list(FiscaleProduct product) {
        startPage();
        List<FiscaleProduct> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出智能商品库列表
     */
    @RequiresPermissions("fiscale:product:export")
    @Log(title = "智能商品库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FiscaleProduct product) {
        List<FiscaleProduct> list = productService.selectProductList(product);
        ExcelUtil<FiscaleProduct> util = new ExcelUtil<>(FiscaleProduct.class);
        util.exportExcel(response, list, "智能商品库数据");
    }

    /**
     * 获取智能商品库详细信息
     */
    @RequiresPermissions("fiscale:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        return success(productService.selectProductById(productId));
    }

    /**
     * 新增智能商品库
     */
    @RequiresPermissions("fiscale:product:add")
    @Log(title = "智能商品库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FiscaleProduct product) {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改智能商品库
     */
    @RequiresPermissions("fiscale:product:edit")
    @Log(title = "智能商品库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FiscaleProduct product) {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除智能商品库
     */
    @RequiresPermissions("fiscale:product:remove")
    @Log(title = "智能商品库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return toAjax(productService.deleteProductByIds(productIds));
    }
    
    /**
     * 导入智能商品库数据
     */
    @RequiresPermissions("fiscale:product:import")
    @Log(title = "智能商品库", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<FiscaleProduct> util = new ExcelUtil<>(FiscaleProduct.class);
        List<FiscaleProduct> productList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = productService.importProduct(productList, updateSupport, operName);
        return success(message);
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<FiscaleProduct> util = new ExcelUtil<>(FiscaleProduct.class);
        util.importTemplateExcel(response, "智能商品库数据");
    }
}